//
//  DYJSerialDateListViewController.m
//  Piece
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "DYJSerialDateListViewController.h"
#import "DYJSerialTableViewCell.h"
#import "ZHNetworkHandler.h"
#import "DYJSerialModel.h"
#import "DYJSerialViewController.h"

@interface DYJSerialDateListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *serialDateListTableView;
@property (nonatomic, strong) NSMutableArray *serialDateArray;


@end

@implementation DYJSerialDateListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self getSerialDateListData];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.title = _dateUrl;
}
#pragma mark -- 创建tableView
- (void)createTableView
{
    _serialDateListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) style:UITableViewStylePlain];
    _serialDateListTableView.separatorStyle = 0;
    _serialDateListTableView.delegate = self;
    _serialDateListTableView.dataSource = self;
    [self.view addSubview:_serialDateListTableView];
    
    //注册cell
    [_serialDateListTableView registerClass:[DYJSerialTableViewCell class] forCellReuseIdentifier:@"serialCell"];
}
#pragma mark -- 数据解析
- (void)getSerialDateListData
{
    //初始化数据源数组
    _serialDateArray = [NSMutableArray array];
    NSString *urlStr = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/serialcontent/bymonth/%@", _dateUrl];
    [ZHNetworkHandler getDataByURLString:urlStr WithBodyString:nil WithHeaderDic:nil WithResponse:0 WithSuccess:^(id result) {
        NSDictionary *dictionary = (NSDictionary *)result;
        NSArray *dataArray = [dictionary valueForKey:@"data"];
        for (NSDictionary *dic in dataArray) {
            DYJSerialModel *serialModel = [[DYJSerialModel alloc] initWithDic:dic];
            [_serialDateArray addObject:serialModel];
        }
        [_serialDateListTableView reloadData];
        
    } WithFailure:^(NSError *error) {
        
    }];
}
#pragma mark -- tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _serialDateArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJSerialModel *shortModel = [_serialDateArray objectAtIndex:indexPath.row];
    //规定字体的大小样式:
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName,nil];
    //得到新的frame
    CGRect shortRect = [shortModel.excerpt boundingRectWithSize:CGSizeMake(ScreenWidth - 90, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return shortRect.size.height + 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJSerialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"serialCell"];
    cell.serialModel = [_serialDateArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJSerialViewController *svc = [[DYJSerialViewController alloc] init];
    DYJSerialModel *shortModel = [_serialDateArray objectAtIndex:indexPath.row];
    svc.serialId = shortModel.id;
    [self.navigationController pushViewController:svc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
